from rest_framework import serializers
from .models import *


# 优惠券的序列化器
class CouponSer(serializers.ModelSerializer):
    # 优惠券类型 名称
    coupon_type_name = serializers.SerializerMethodField(read_only=True)

    def get_coupon_type_name(self, obj):
        return obj.coupon_type.type_name

    # 商品的名称
    good_name = serializers.SerializerMethodField(read_only=True)
    def get_good_name(self, obj):
        if obj.good:
            return obj.good.name
        return ""

    class Meta:
        model = Coupon
        fields = "__all__"
        read_only_fields = ['id', ]


# 优惠券类型的序列化器
class CouponTypeSer(serializers.ModelSerializer):

    class Meta:
        model = CouponType
        fields = "__all__"
        read_only_fields = ["id", ]